package cn.js.sip.rat.system.shiro.realm;

import cn.js.sip.rat.system.entity.SysMenuEntity;
import cn.js.sip.rat.system.entity.SysRoleEntity;
import cn.js.sip.rat.system.entity.SysUserEntity;
import cn.js.sip.rat.system.service.SysMenuService;
import cn.js.sip.rat.system.service.SysRoleService;
import cn.js.sip.rat.system.service.SysUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: cn.js.sip.rat.system.shiro.realm
 * @Description: TODO
 * @author: xuxiaoli
 * @Date: 2019-12-27   19:33
 */
public class UserRealm extends AuthorizingRealm {
    private static Logger logger = LoggerFactory.getLogger(UserRealm.class);
    @Resource(name = "sysUserService")
    private SysUserService sysUserService;
    @Resource(name = "sysRoleService")
    private SysRoleService sysRoleService;
    @Resource(name = "sysMenuService")
    private SysMenuService sysMenuService;

    /**
     * 授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SysUserEntity userEntity = (SysUserEntity) principalCollection.getPrimaryPrincipal();
        if(userEntity==null){
            return null;
        }
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        if (userEntity.isAdmin()) {
            authorizationInfo.addRole("admin");
            authorizationInfo.addStringPermission("*:*:*");
        }else{
            List<SysRoleEntity> roles = sysRoleService.getRolesByUserId(userEntity.getId());
            List<SysMenuEntity> menus= sysMenuService.getMenusByUser(userEntity);
            // 角色加入AuthorizationInfo认证对象
            authorizationInfo.setRoles(roles.stream().map(SysRoleEntity::getRoleCode).collect(Collectors.toSet()));
            // 权限加入AuthorizationInfo认证对象
            authorizationInfo.setStringPermissions(menus.stream().map(SysMenuEntity::getPerms).collect(Collectors.toSet()));
        }
        return authorizationInfo;
    }

    /**
     * 认证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;
        String username = upToken.getUsername();
        String password = "";
        if (upToken.getPassword() != null) {
            password = new String(upToken.getPassword());
        }
        SysUserEntity user = null;
        user = sysUserService.checkUser(username);
        if (user == null) {
            throw new AuthenticationException("用户名不存在");
        }
        SimpleHash simpleHash = new SimpleHash("MD5", password, null, 3);
        if(!user.getPassword().equals(simpleHash.toString())){
            throw new AuthenticationException("密码不存在");
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), getName());
        return info;
    }
}
